package org.example.java8.future;

import java.util.concurrent.*;

public class FutureInAction3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ExecutorService executorService= Executors.newSingleThreadExecutor();
        Future<String> future = executorService.submit(() -> {
            try {
                Thread.sleep(4000L);
                return "i am finished";
            } catch (InterruptedException e) {
                return "error";
            }
        });
        //阻塞
        //String value = future.get(10, TimeUnit.SECONDS);
        //System.out.println(value);

        while (!future.isDone()){
            Thread.sleep(10);
        }
        System.out.println(future.get());
        executorService.shutdown();
        //shutdownNow会立即执行关闭，会返回一个没有执行结束的任务的list
        //executorService.shutdownNow();
    }
}
